package com.sgb.kjwl.view.widget.lease.common;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.VideoBean;
import com.sgb.kjwl.databinding.ItemContentVoidLiseViewBinding;

import com.swgk.core.view.entity.VideoListEntity;
import com.sgb.knowledgeshare.view.activity.PlayListActivity;
import com.sgb.kjwl.view.widget.lease.common.loadingview.DefaultHorizontalLoadingView;
import com.swgk.core.base.model.entity.BaseEntity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 张驰
 * @date 2021/6/18 10:49
 * @Description 租赁经营-自定义视频布局列表
 * @name com.swgk.core.customview
 */
public class ContentVoidLiseView extends RelativeLayout {

    private Context mContext;
    private ItemContentVoidLiseViewBinding binding;
    //视频
    private HomeLeaseVideoAdapter mVideoAdapter;
    //视频加载更多视图
    private DefaultHorizontalLoadingView mFooterView;
    private boolean hasMoreData = false;
    private boolean isLoading = true;
    public int page = 0;
    public int pageSize = 10;

    public ContentVoidLiseView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initView();
    }

    private void initView() {
        binding = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.item_content_void_lise_view, this, true);
        initVideo();
        getVideo(false);
    }


    private void initVideo() {
        mFooterView = new DefaultHorizontalLoadingView(getContext());
        if (mVideoAdapter == null) {
            mVideoAdapter = new HomeLeaseVideoAdapter(getContext());
            mVideoAdapter.addFooterView(mFooterView);
        }
        binding.rvVideo.setLayoutManager(new LinearLayoutManager(getContext(), RecyclerView.HORIZONTAL, false));
        binding.rvVideo.setAdapter(mVideoAdapter);
        //binding.rvVideo.setHasFixedSize(true);
        binding.rvVideo.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    boolean lastItemFullVisible = RecyclerViewUtils.isLastItemFullVisible(binding.rvVideo);
                    if (hasMoreData && !isLoading && lastItemFullVisible) {
                        mFooterView.onLoading();
                        isLoading = true;
                        getVideo(true);
                    } else if (lastItemFullVisible && mVideoAdapter.getData().size() > 10) {
                        onPageScroll();
                    }
                }
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

            }
        });
        mVideoAdapter.setOnItemClickListener(new CommonBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, View itemView) {
                Intent intent = new Intent(mContext, PlayListActivity.class);
                intent.putExtra(PlayListActivity.VIDEOLISTBEAN, (Serializable) mVideoAdapter.getData());
                intent.putExtra(PlayListActivity.PSO, position);
                intent.putExtra(PlayListActivity.VIDEOTYPE, "home_lease");
                intent.putExtra(PlayListActivity.PAGE, 1);
                mContext.startActivity(intent);
            }
        });
    }

    public void getViedoList(List<VideoBean> result, boolean isLoadMore) {
        isLoading = false;
        if (!isLoadMore) {
            if (result.isEmpty()) {
                binding.rvVideo.setVisibility(View.GONE);
                return;
            }
        }
        binding.rvVideo.setVisibility(View.VISIBLE);
        if (result.size() < 10) {
            hasMoreData = false;
            mFooterView.onComplete();
        } else {
            hasMoreData = true;
            mFooterView.onLoaded();
        }
        if (isLoadMore) {
            mVideoAdapter.appendItems(result);
        } else {
            mVideoAdapter.setData(result);
        }

    }

    private void onPageScroll() {
        Intent intent = new Intent(mContext, PlayListActivity.class);
        intent.putExtra(PlayListActivity.VIDEOLISTBEAN, (Serializable) mVideoAdapter.getData());
        intent.putExtra(PlayListActivity.PSO, 0);
        intent.putExtra(PlayListActivity.VIDEOTYPE, "home_lease");
        intent.putExtra(PlayListActivity.PAGE, 1);
        mContext.startActivity(intent);
    }

    /**
     * 租赁视频列表
     */
    public void getVideo(boolean isLoadMore) {
        Map videoMap = new HashMap<>();
        page = isLoadMore ? page + 1 : 1;
        videoMap.put("page", page);
        videoMap.put("limit", pageSize);
        videoMap.put("name", "经营租赁");
        videoMap.put("parentName", "网站经营");
        APIManager.getInstance().getHomeLeaseAPI().getHomeVideoList(videoMap).enqueue(new ICallBack<BaseEntity<VideoListEntity>>() {
            @Override
            public void onSuccess(BaseEntity<VideoListEntity> data) {
                if (data.getData() != null) {
                    List<VideoBean> videoBeans = data.getData().getRecords();
                    getViedoList(videoBeans, isLoadMore);
                }
            }

            @Override
            public void onFail() {
                super.onFail();
                getViedoList(new ArrayList<>(), isLoadMore);
            }
        });
    }
}
